package cn.kow.kmall.controller.content;

import cn.kow.kmall.common.enums.ContentModuleTypeEnum;
import cn.kow.kmall.controller.BaseController;
import cn.kow.kmall.core.response.KmallResult;
import cn.kow.kmall.pojo.dto.ContentModuleImageDTO;
import cn.kow.kmall.pojo.entity.ContentModule;
import cn.kow.kmall.pojo.entity.ContentModuleImage;
import cn.kow.kmall.service.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2020-04-28 16:35
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("/content/banner")
public class BannerController extends BaseController {

    @Autowired
    private ContentService contentService;

    @PostMapping(value = "/list")
    public KmallResult list() {
        List<ContentModule> modules = contentService.listModuleByType(ContentModuleTypeEnum.BANNER.getType());
        if (CollectionUtils.isEmpty(modules)) {
            return KmallResult.success(null);
        }
        ContentModule module = modules.get(0);
        List<ContentModuleImage> list = contentService.listModuleImage(module.getId());
        return KmallResult.success().put("moduleId", module.getId()).put("list", list);
    }

    @PostMapping(value = "/save")
    public KmallResult save(@RequestBody ContentModuleImageDTO imageDTO) {
        contentService.saveModuleImage(imageDTO, currentUser().getUsername());
        return KmallResult.success();
    }

    @PostMapping("/del")
    public KmallResult remove(@RequestBody Long[] ids) {
        contentService.deleteModuleImage(Arrays.asList(ids));
        return KmallResult.success();
    }
}
